'''

json
'''

import json
d = dict(name='小明',age = 20)
s = json.dumps(d,ensure_ascii=False)
print('json:',s)
print("字符串转换成对象OBJ")
print(json.loads(s))

class Student(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

s = Student('张三哇',23,'男孩')

def student2dict(std):
    return {
        'name':std.name,
        'age':std.age,
        'score':std.sex
    }

print("类对象进行JSON转换")
print(json.dumps(s,default=student2dict,ensure_ascii=False))

#吧任意实例变为dict
print(json.dumps(s,default = lambda obj: obj.__dict__))

ajson = json.dumps(s,default=lambda obj:obj.__dict__)
def dict2studnt(d):
    return Student(d['name'],d['age'],d['sex'])

ss = json.loads(ajson,object_hook=dict2studnt)

print("student对象name:",ss.name)
